package com.mdp.menu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.err.BizException;
import com.mdp.core.service.BaseService;
import com.mdp.core.utils.DateUtils;
import com.mdp.menu.entity.MenuFavorite;
import com.mdp.menu.mapper.MenuFavoriteMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com  顶级模块 mdp 大模块 menu 小模块 <br>
 * 实体 MenuFavorite 表 menu_favorite 当前主键(包括多主键): favoriteid; 
 ***/
@Service("mdp.menu.menuFavoriteService")
public class MenuFavoriteService extends BaseService<MenuFavoriteMapper,MenuFavorite> {
	static Logger logger =LoggerFactory.getLogger(MenuFavoriteService.class);
	@Autowired
	private MenuDefService menuDefService;

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}

	/**
	 * 快捷入口的添加
	 *
	 * @author 林钰坤
	 * @cdate 2019/1/24 20:10
	 */
	public void saveMenuFavorites(Map<String, Object> params) {
		List<Map<String, Object>> menuFavoriteListMap = (List) params.get("menuFavorites");
		Map<String, Object> userInfoMap = (Map) params.get("userInfo");
		String deptid = (String) userInfoMap.get("deptid");
		String userid = (String) userInfoMap.get("userid");
		String branchId = (String) userInfoMap.get("branchId");
		if (!StringUtils.hasText(userid)) {
			throw new BizException("用户名不能为空");
		}
		if (!StringUtils.hasText(deptid)) {
			throw new BizException("请传递部门编号");
		}
		if (!StringUtils.hasText(branchId)) {
			throw new BizException("请传递机构编号");
		}
		/*
		 * MenuFavorite getUserBeforeSetExpressEntry=new MenuFavorite();
		 * getUserBeforeSetExpressEntry.setUserid(userid);//用户id
		 * getUserBeforeSetExpressEntry.setDeptid(deptid);//部门id List<MenuFavorite>
		 * UserBeforeMenuFavoriteList=this.selectListByWhere(
		 * getUserBeforeSetExpressEntry);//得到某个用户的设置的快捷入口 int
		 * UserBeforeMenuFavoriteLength=0;//用户之前设置的快捷入口的数量 int
		 * UserBeforeMenuFavoriteIndex=0;
		 * if(!CollectionUtils.isEmpty(UserBeforeMenuFavoriteList)) {//如果获取出来的对象不为空
		 * UserBeforeMenuFavoriteLength=UserBeforeMenuFavoriteList.size();//赋值 }
		 */
		/*
		 * MenuFavorite customerMenuFavoriteSelectObject=new MenuFavorite();//自定义的删除对象
		 * customerMenuFavoriteSelectObject.setUserid(userid);
		 * customerMenuFavoriteSelectObject.setDeptid(deptid);
		 * customerMenuFavoriteSelectObject.setBranchId(branchId); List<MenuFavorite>
		 * customerMenuFavoriteSelectObjectList=this.selectListByWhere(
		 * customerMenuFavoriteSelectObject); for(int
		 * i=0;i<customerMenuFavoriteSelectObjectList.size();i++) {//获取出数据库中的内容
		 *
		 * }
		 */
		MenuFavorite customerMenuFavoriteDeleteObject = new MenuFavorite();// 自定义的删除对象
		customerMenuFavoriteDeleteObject.setUserid(userid);
		customerMenuFavoriteDeleteObject.setDeptid(deptid);
		customerMenuFavoriteDeleteObject.setBranchId(branchId);
		this.deleteByWhere(customerMenuFavoriteDeleteObject);
		for (int i = 0; i < menuFavoriteListMap.size(); i++) {// 遍历
			Map<String, Object> mf = menuFavoriteListMap.get(i);
			String mfName =null;
			String accUrl = (String) mf.get("accUrl");// 二级路径路径
			String favoriteid = (String) mf.get("favoriteid");// 二级路径路径
			String menuname = (String) mf.get("menuname");// 二级路径名字
			String iconUrl = (String) mf.get("icon");// 二级路径名字
			String mfPath = null;
			if (!StringUtils.hasText(accUrl)) {// 如果为空
				mfPath = (String) mf.get("path");// 二级路径路径
				mfName= (String) mf.get("name");// 二级路径名字
			} else {
				mfPath = accUrl;
				mfName=menuname;
			}
			MenuFavorite menuFavorite = new MenuFavorite();
			if (!StringUtils.hasText(favoriteid)) {// 如果是空的
				favoriteid = createKey("favoriteid");
			}
			menuFavorite.setFavoriteid(favoriteid);// 设置主键
			menuFavorite.setAccUrl(mfPath);// 设置路径
			menuFavorite.setBranchId(branchId);// 机构编号
			menuFavorite.setMenuname(mfName);// 设置菜单名字
			menuFavorite.setDeptid(deptid);// 设置部门编号
			menuFavorite.setUserid(userid);// 设置创建人
			menuFavorite.setCuserid(userid);// 得到用户id
			menuFavorite.setLopuserid(userid);// 设置修改人
			menuFavorite.setIcon(iconUrl);
			Date date = DateUtils.getNowDate();
			menuFavorite.setCdate(date);// 创建时间
			menuFavorite.setLopcreate(date);// 修改时间
			this.insert(menuFavorite);// 插入对象
			/* } */
		}
	}

	public void batchSaveMenuFavorites(List<Map<String, Object>> params, String userid) {
		//先删除
		if (!StringUtils.hasText(userid)) {
			throw new BizException("用户名不能为空");
		}
		MenuFavorite customerMenuFavoriteDeleteObject = new MenuFavorite();// 自定义的删除对象
		customerMenuFavoriteDeleteObject.setUserid(userid);
		this.deleteByWhere(customerMenuFavoriteDeleteObject);
		//添加modules
		for (Map<String, Object> param : params) {
			MenuFavorite menuFavorite = new MenuFavorite();
			menuFavorite.setFavoriteid(createKey("favoriteid"));
			menuFavorite.setBranchId((String) param.get("branchId"));// 机构编号
			menuFavorite.setMenuname((String) param.get("menuname"));// 设置菜单名字
			menuFavorite.setMenuid((String) param.get("menuid"));
			menuFavorite.setDeptid((String) param.get("deptid"));// 设置部门编号
			menuFavorite.setUserid(userid);// 设置创建人
			menuFavorite.setCuserid(userid);// 得到用户id
			menuFavorite.setLopuserid(userid);// 设置修改人
			menuFavorite.setIcon((String) param.get("icon"));
			this.insert(menuFavorite); //插入对象
		}
	}

	/**
	 * 得到快捷入口所有的路径
	 *
	 * @author 林钰坤
	 * @cdate 2019/1/24 20:51
	 */
	public List<MenuFavorite> getMenuFavorites(String userid) {

		MenuFavorite customerMenuFavoriteQueryObject = new MenuFavorite();
		customerMenuFavoriteQueryObject.setUserid(userid);// 设置用户
		List<MenuFavorite> menuFavoriteList = this.selectListByWhere(customerMenuFavoriteQueryObject);
		return menuFavoriteList;
	}

	/**
	 * 新增快捷入口的图标
	 *
	 * @author 林钰坤
	 * @cdate 2019/1/25 14:02
	 */
	public void addMenuFavoriteIcon(Map<String, Object> params) {
		Map<String, Object> menuFavorite = (Map) params.get("menuFavorite");
		String iconUrl = (String) params.get("iconUrl");
		String favoriteid = (String) menuFavorite.get("favoriteid");
		if (!StringUtils.hasText(favoriteid)) {
			throw new BizException("请传入favoriteid");
		}
		MenuFavorite customerMenuFavoriteUpdateObject = new MenuFavorite();
		customerMenuFavoriteUpdateObject.setFavoriteid(favoriteid);
		customerMenuFavoriteUpdateObject.setIcon(iconUrl);
		this.updateSomeFieldByPk(customerMenuFavoriteUpdateObject);
	}






}

